import { useEffect } from 'react';
import type { ISMap, ISMapEventCallbackParameter, ISMapEventType } from '@/compnents';

export const useMapEventListener = <T extends keyof ISMapEventType>(
  map?: ISMap,
  event?: T,
  callback?: (e: ISMapEventCallbackParameter<T>) => void,
) => {
  useEffect(() => {
    if (!map || !event || !callback) return () => {};
    map.on(event, callback);
    return () => {
      map.off(event, callback);
    };
  }, [map, event, callback]);
};

export const useMapEventOnceListener = <T extends keyof ISMapEventType>(
  map?: ISMap,
  event?: T,
  callback?: (e: ISMapEventCallbackParameter<T>) => void,
) => {
  useEffect(() => {
    if (!map || !event || !callback) return () => {};
    map.once(event, callback);
    return () => {
      map.off(event, callback);
    };
  }, [map, event, callback]);
};
